﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {
    $('#list').datagrid({
        //title: '知识库管理',
        idField: 'KnowledgeID',
        autoRowHeight: true,
        singleSelect: true, 
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'Knowledge/GetList',
        view: knowledgeView,
        showHeader: false,
        columns: [[
			{ field: 'ck', checkbox: false, hidden: true },
			{ field: 'KnowledgeID', title: 'ID', hidden: true},
			{ field: 'KnowledgeCode', title: '编号', width: 50, hidden: true },
			{ field: 'KnowledgeName', title: '名称', width: 180, hidden: true },
			{ field: 'Tag', title: '标签', width: 180, hidden: true },
			{ field: 'DictionaryKind', title: '类型', width: 180},
			{ field: 'HaveAttachment', title: '是否有附件', width: 180, hidden: true },
			{ field: 'Content', title: '内容', width: 180 },
			{ field: 'Status', title: '状态', width: 180, hidden: true },
			{ field: 'CreateBy', title: '创建人', width: 180, hidden: true },
			{ field: 'CreateTime', title: '创建时间', width: 180, hidden: true },
			{ field: 'UpdateBy', title: '修改人', width: 180, hidden: true },
			{ field: 'UpdateTime', title: '修改时间', width: 180, hidden: true },
		
        ]],
        onClickRow: function (value, row, index) {
            GetKnowledgeInfo(row);
        }
    });
}
//新闻列表显示视图
var knowledgeView = $.extend({}, $.fn.datagrid.defaults.view, {
    renderRow: function (target, fields, frozen, rowIndex, rowData) {
        var cc = [];
        cc.push('<td colspan=' + fields.length + ' style="border:1;width:780px;">');
        if (!frozen) {
            cc.push('<div>');
            cc.push('<span style="font-weight:bold;padding-left:5px">[' + rowData["KnowledgeKindName"] + ']</span><span style="padding-left:5px">' + rowData["KnowledgeName"] + '</span> ');
            cc.push('</div>');
        }
        cc.push('</td>');
        return cc.join('');
    }
});
//根据公告ID获取公共信息
function GetKnowledgeInfo(row) {
    var strTitle = row.KnowledgeName;//标题
    var strContent = row.Content;//内容
    var publishtime = row.CreateTime;//发布时间
    var publisher = row.CreateBy;//发布人
    var Lastpublishtime = row.UpdateTime;//发布时间
    var Lastpublisher = row.UpdateBy;//发布人

    var reg = new RegExp("\r\n", "g"); //创建正则RegExp对象  
    if (row.CreateTime != null) publishtime = formatDate(publishtime, "yyyy年MM月dd");

    if (row.UpdateBy != null) Lastpublisher = row.UpdateBy;
    else Lastpublisher = row.CreateBy;

    if (row.UpdateTime != null) Lastpublishtime = formatDate(Lastpublishtime, "yyyy年MM月dd");
    else Lastpublishtime = formatDate(publishtime, "yyyy年MM月dd");

    $('#Name').html(strTitle);
    $('#PublishTime').html(publishtime);
    $('#Publisher').html(publisher);
    $('#LastModifyTime').html(Lastpublishtime);
    $('#LastModifyPerson').html(Lastpublisher);
    $('#KnowledgeContent').html(strContent.replace(reg, "<br/>"));
}
//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    url = '/Knowledge/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#fm').form('load', row);
        url = '/Knowledge/Edit';
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/Knowledge/Delete', { id: rows.KnowledgeID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}

//搜索
function doSearch() {
    $('#list').datagrid('load', {
        KnowledgeName: $('#KnowledgeName').val(),
        DictionaryKind: $('#DictionaryKind').combobox('getValue')
    });
}